package com.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class JDBCUtil {
    private static String diver;
    private static String url;
    private static String username;
    private static String password;

    static {
        try {
        ResourceBundle bundle=ResourceBundle.getBundle("db");
        diver= bundle.getString("driverClassName");
        url= bundle.getString("url");
        username= bundle.getString("username");
        password= bundle.getString("password");
        //加载数据库驱动
        Class.forName(diver);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    //获取连接方法
    public static Connection getConnection(){
        Connection connection=null;
        try {
            connection= DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return connection;
    }

    //关闭连接
    public static void closeConnection(Connection connection){
        try {
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //事务回滚
    public static void rollbackConnection(Connection connection){
        try {
            connection.rollback();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
